#!/usr/bin/env jruby

require 'test/unit'
dir = File.dirname(__FILE__)
require File.join(dir, '..', '..', 'lib', 'monkey', 'string')

########################################

class TestStringCharsIn < Test::Unit::TestCase
  FOO123 = 'foo123'
  
  def test_should_validate_arg
    assert_raise(TypeError) { FOO123.chars_in?(123) }
    assert_nothing_thrown { FOO123.chars_in?('') }
  end
  
  def test_should_return_boolean
    result1 = FOO123.chars_in?(FOO123)
    assert (result1.is_a?(TrueClass) or result1.is_a?(FalseClass)), '1'
    result2 = FOO123.chars_in?('')
    assert (result2.is_a?(TrueClass) or result2.is_a?(FalseClass)), '2'
  end
  
  def test_should_return_true
    assert_equal true, FOO123.chars_in?(FOO123), 'as-is'
    assert_equal true, FOO123.chars_in?(FOO123.squeeze), 'squeezed'
    assert_equal true, FOO123.chars_in?('EoMc8T' + FOO123), 'extra'
  end
  
  def test_should_return_false
    assert_equal false, FOO123.chars_in?(''), 'empty string'
    assert_equal false, FOO123.chars_in?('foo'), 'foo'
    assert_equal false, FOO123.chars_in?('bar'), 'bar'
  end
end

########################################

class TestStringAllChars < Test::Unit::TestCase
  CHAR_MAXNUM = 256
  RESULT = String.all_chars
  
  def test_should_return_array
    assert_kind_of Array, RESULT
  end
  
  def test_should_return_with_correct_length
    assert_equal CHAR_MAXNUM, RESULT.size
  end
  
  def test_should_return_unique_chars
    assert_equal RESULT.size, RESULT.uniq.size
  end
  
  def test_should_return_ordered_chars
    0.upto(RESULT.size-2) do |i|
      i1, i2 = i, i+1
      r1, r2 = RESULT[i1], RESULT[i2]
      #puts "#{i1} < #{i2}"
      #puts "#{r1} < #{r2}"
      assert (r1 < r2), "#{r1} !< #{r2}"
    end
  end
end
